Home |
| Latest | About | Random
# Answers to Scope problem All the errors lead to build errors, as they use variables that is not defined in the scope. ```cpp #include using namespace std; int main() { int a = 1; cout << a << endl; // 1 // int a = 100; // CANNOT REDECLARE AN ALREADY DECLARED VARIABLE { int b = 2; a = a + b; cout << a << endl; // 3 int a = 100; cout << a << endl; // 100 a = a + b; cout << a << endl; // 102 { int c = 3; cout << a << endl; // 102 cout << b << endl; // 2 cout << c << endl; // 3 } cout << a << endl; // 102 cout << b << endl; // 2 // cout << c << endl; // NO c DECLARED IN THIS SCOPE! { // cout << c << endl; // NO c DECLARED IN THIS SCOPE, YET! int c = 300; cout << c << endl; // 300 } } cout << a << endl; // 3 // cout << b << endl; // NO b DECLARED IN THIS SCOPE! // cout << c << endl; // NO c DECLARED IN THIS SCOPE! } ``` And the output, after the build errors have been commented out, we get output ```cmd 1 3 100 102 102 2 3 102 2 300 3 ```